package com.zztl.infiniteBand.util

import android.content.Context
import android.media.MediaPlayer

import com.zztl.infiniteBand.R


/**
 * @desc
 * @auth ${user}
 * @time 2018/4/23  17:13
 */

//开始播放声音
object PlayVoice {
    private var mediaPlayer: MediaPlayer? = null

    fun playVoice(context: Context) {

        if (null == mediaPlayer) {
            mediaPlayer = MediaPlayer.create(context, R.raw.voice_1)
        }
        var isPlaying = false
        try {
            isPlaying = mediaPlayer!!.isPlaying
        } catch (e: IllegalStateException) {
            mediaPlayer = null
            mediaPlayer = MediaPlayer.create(context, R.raw.voice_1)
        }

        if (isPlaying) {
            mediaPlayer!!.stop()
            mediaPlayer!!.release()
            mediaPlayer = null
            mediaPlayer = MediaPlayer.create(context, R.raw.voice_1)
        }
        mediaPlayer!!.start()
        mediaPlayer!!.setOnCompletionListener { }

    }

    //停止播放声音
    fun stopVoice() {
        if (null != mediaPlayer && mediaPlayer!!.isPlaying) {
            mediaPlayer!!.stop()
        }
        mediaPlayer!!.release()
        mediaPlayer = null
    }
}
